package com.prawn.ift.datasearch.controller;
import java.util.List;
import java.util.Map;

import com.prawn.ift.datasearch.client.IftCollEquipmentClient;
import com.prawn.ift.datasearch.pojo.DataRecord;
import com.prawn.ift.datasearch.pojo.DataRecordWithCheckItem;
import com.prawn.ift.datasearch.service.DataRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import util.IdWorker;
import util.NullAttributeUtils;

import javax.xml.crypto.Data;

/**
 * 控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/dataRecord")
public class DataRecordController {

	@Autowired
	private DataRecordService dataRecordService;

	@Autowired
	private IftCollEquipmentClient iftCollEquipmentClient;

	@Autowired
	private IdWorker idWorker;

	/**
	 * 测试调用设备采集模块
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/equipment/{id}",method= RequestMethod.GET)
	public Result findCollEquipmentById(@PathVariable("id") String id) {
		return iftCollEquipmentClient.findById(id);
	}

	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true,StatusCode.OK,"查询成功",dataRecordService.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",dataRecordService.findById(id));
	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<DataRecord> pageList = dataRecordService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<DataRecord>(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",dataRecordService.findSearch(searchMap));
    }

	/**
	 * 分页+多条件查询——预测使用接口
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/searchforpredict/{page}/{size}",method=RequestMethod.POST)
	public PageResult<DataRecord> findSearchForPredict(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<DataRecord> pageList = dataRecordService.findSearch(searchMap, page, size);
		PageResult<DataRecord> dataRecordPageResult = new PageResult<>(pageList.getTotalElements(), pageList.getContent());
		return dataRecordPageResult;
	}
	
	/**
	 * 增加
	 * @param dataRecord
	 */
	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody DataRecord dataRecord  ){
		dataRecord.setId(String.valueOf(idWorker.nextId()));
		dataRecordService.add(dataRecord);
		return new Result(true,StatusCode.OK,"增加成功");
	}
	
	/**
	 * 修改
	 * @param dataRecord
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody DataRecord dataRecord, @PathVariable String id ){
		DataRecord dbRecord=dataRecordService.findById(id);
		BeanUtils.copyProperties(dataRecord,dbRecord, NullAttributeUtils.getNullPropertyNames(dataRecord));
		dataRecordService.update(dbRecord);
		return new Result(true,StatusCode.OK,"修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		dataRecordService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}
	/**
	 * 分页查询数据记录信息列表分页从 1开始
	 * @return LayuiDataGridResult
	 */
	@RequestMapping(value = "/page/relation/{page}/{limit}",method = RequestMethod.GET)
	public Result getPageWithRelation(@PathVariable(value = "page") int page, @PathVariable(value = "limit")int limit) {
		Page<DataRecordWithCheckItem> pageList=dataRecordService.getDataRecordPageAndRelation(page,limit);
		return  new Result(true,StatusCode.OK,"查询成功",new PageResult<DataRecordWithCheckItem>(pageList.getTotalElements(), pageList.getContent()));
	}

	/**
	 * 多条件分页查询数据记录信息列表,分页从 1开始
	 * @return LayuiDataGridResult
	*/
	@RequestMapping(value = "/page/relation/query/{page}/{limit}",method = RequestMethod.POST)
	public Result getPageWithRelationByVo(@PathVariable(value = "page") int page, @PathVariable(value = "limit")int limit,@RequestBody Map<String,Object> dataRecordVo) {
		Page<DataRecordWithCheckItem> pageList=dataRecordService.getDataRecordPageAndRelationByVo(page,limit,dataRecordVo);
		return new Result(true,StatusCode.OK,"查询成功",new PageResult<DataRecordWithCheckItem>(pageList.getTotalElements(), pageList.getContent()));
	}
}
